package TuiLayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;

/**
 * Vestbjerg project
 * @GroupB2 (UCN - Computer Science DMAJ0914) 
 */
public class Menu
{
    private ProductUI productUI;
    private MachineryUI machineryUI;
    private LeaseUI leaseUI;
    private CustomerUI customerUI;
    private EmployeeUI employeeUI;
    private PurchaseUI purchaseUI;
    private Employee employee;
    private StatisticsUI statisticsUI;

    private static String masterPassword = "1234";

    public Menu()
    {
        productUI = new ProductUI();
        machineryUI = new MachineryUI();
        leaseUI = new LeaseUI();
        customerUI = new CustomerUI();
        employeeUI = new EmployeeUI();
        purchaseUI = new PurchaseUI();
        statisticsUI = new StatisticsUI();

        login();

        //menuStart();
    }

    public static void setMasterPassword(String newMasterPassword) 
    {
        masterPassword = newMasterPassword;
    }

    public static String getMasterPassword()     
    {
        return masterPassword;
    }

    private int getChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        return choice;
    }

    private void login() 
    {   
        boolean exit = false;
        while(!exit) {
            boolean access = false;
            boolean errorMsg = false;
            while(access == false && exit == false) {

                if(errorMsg) 
                {
                    System.out.println("\f******************************************");
                    System.out.println("*********** V E S T B J E R G ************");
                    System.out.println("***** N I L L I H C  S Y S T E M  1.0 ****"); 
                    System.out.println("******************************************");
                    System.out.println("--");
                    System.out.println("** Error: Invalid ID. Try again or type 'quit' to quit:");
                }
                else
                {
                    System.out.println("\f******************************************");
                    System.out.println("*********** V E S T B J E R G ************");
                    System.out.println("***** N I L L I H C  S Y S T E M  1.0 ****");
                    System.out.println("******************************************");
                    System.out.println("--");
                    System.out.println("** Enter your employee ID (type 'quit' to quit): ");
                }
                Scanner sc = new Scanner(System.in);
                String id = sc.nextLine();
                if(id.equals("quit")) 
                {
                    exit = true;
                    System.out.println("\f************************************");  
                    System.out.println("**  The system is safe to leave!  **");
                    System.out.println("************************************"); 
                }
                else {
                    Employee foundEmployee = employeeUI.findEmployee(id);
                    if(foundEmployee != null) 
                    {
                        this.employee = foundEmployee;
                        access = true;

                    }
                    else 
                    {
                        errorMsg = true;
                    }
                }

            }

            if(!exit) 
            {
                menuStart();
            }

        }
    }
    // Main menu loop
    public void menuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                purchaseUI.setSecurityLvl(employee.getSecurityLvl());
                purchaseUI.setEmployee(employee);
                purchaseUI.menuStart();
            }
            else if(choice == 2)
            {
                leaseUI.setSecurityLvl(employee.getSecurityLvl());
                leaseUI.menuStart();
            }
            else if(choice == 3)
            {
                customerUI.menuStart();
            }
            else if(choice == 4 && employee.getSecurityLvl() == 3)
            {
                administrationMenu();
            }
            else if(choice == 0) 
            {
                exit = true;
                System.out.println("\f\n\n\n     ************************************");  
                System.out.println("     **  The system is safe to leave!  **");
                System.out.println("     ************************************"); 
            }
            else 
            {
                print = "ERROR!";
            }

        }
    }

    public void writeMenu(String print) 
    {
        System.out.println("\f *** Main Menu ***");
        System.out.println(" (1)Sale");
        System.out.println(" (2)Lease");
        System.out.println(" (3)Customers");
        if(employee.getSecurityLvl() == 3)        
        {
            System.out.println(" (4)Administration");
        }

        System.out.println(" (0)Close");

        if(print != null) 
        {
            System.out.println("\n " + print);
        }
        System.out.print("\n Make your choice: ");
    }

    public void administrationMenu()
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeAdministrationMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                productUI.menuStart();
            }
            else if(choice == 2)
            {
                machineryUI.menuStart();
            }
            else if(choice == 3) 
            {
                employeeUI.menuStart();
            }
            else if(choice == 4) 
            {
                statisticsUI.menuStart();
            }
            else if(choice == 5) 
            {
                print = setMasterPassword();
            }
            else if(choice == 6) 
            {
                print = setDiscountCap();
            }
            else if(choice == 0) 
            {
                exit = true;
            }
        }
    }

    public void writeAdministrationMenu(String print)
    {
        System.out.println("\f *** Administration ***");
        System.out.println(" (1)Products");
        System.out.println(" (2)Machinery");
        System.out.println(" (3)Employees");
        System.out.println(" (4)Statistics");
        System.out.println(" (5)Change master password");
        System.out.println(" (6)Change sale discount cap");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    public String setMasterPassword() 
    {
        String print = null;

        System.out.println("Enter current password:");
        Scanner sc = new Scanner(System.in);

        String currentPassword = sc.nextLine();

        if(currentPassword.equals(getMasterPassword())) 
        {
            System.out.println("Enter new password: ");
            String newPassword = sc.nextLine();

            System.out.println("Enter new password again: ");
            String passwordAgain = sc.nextLine();

            if(newPassword.equals(passwordAgain)) 
            {
                setMasterPassword(newPassword);
                print = "Password has been changed successfully.";
            }
            else 
            {
                print = "Password do not match.";
            }
        }
        else 
        {
            print = "Error: Incorrect password.";
        }
        return print;
    }

    public String setDiscountCap() 
    {
        String print = null;

        System.out.println("Current sale discount cap: " + Purchase.getMaxDiscount() + "%") ;
        System.out.println("Enter new discount cap: ");
        Scanner sc = new Scanner(System.in);
        int cap = sc.nextInt();
        try {
            if(cap >= 0 && cap <= 100) 
            {
                Purchase.setMaxDiscount(cap);
                print = "Discount cap has been changed to " + Purchase.getMaxDiscount() + "%.";
            }
            else 
            {
                print = "Error: Wrong input.";
            }
        }
        catch(NumberFormatException e) 
        {
            print = "Error: Wrong input.";
        }

        return print;
    }
}